#include <stdlib.h>

#define BUFHDRSIZE (sizeof(int)*2)
#define BUFSIG 0xafaf

int bytes_alloc = 0; // keeps track of how many bytes have been allocated

void *my_malloc(size_t size)
{
  void *buf;
  unsigned int *buf_header;
  buf_header = buf = malloc(size+BUFHDRSIZE);
  if (buf) bytes_alloc += size;
  *buf_header = BUFSIG;
  *(buf_header + 1) = size;
  return buf+BUFHDRSIZE;
}

void my_free(void *buf)
{
  unsigned int *buf_header;

  buf_header = (void *)buf - BUFHDRSIZE;
  if (*buf_header == BUFSIG) {
    bytes_alloc -= *(buf_header + 1);
    *buf_header = 0;
    free(buf_header);
  } else {
    fprintf(stderr, "Alert: Memory corruption or attempt to free twice: buffer = %u\n", buf);
  }
}
